
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/request/token'
import $message from '@/utils/message'
Vue.use(VueRouter)

/* // 重写push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
} */

const routes = [
  {
    path: '/', // 首页
    name: 'index',
    redirect: '/home',
    component: () => import('@/views/Index.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: "揽山行-首页",
          id: 1
        },
        component: () => import('@/views/Home.vue')
      },
      {
        path: '/recommend',  // 推荐
        name: 'recommend',
        meta: {
          title: "揽山行-推荐",
          id: 2
        },
        component: () => import('@/views/Recommond.vue')
      },
      {
        path: '/community',  // 社区
        name: 'community',
        meta: {
          title: "揽山行-社区",
          id: 3
        },
        component: () => import('@/views/Community.vue')
      },
      {
        path: '/specialty',   // 特产
        name: 'specialty',
        meta: {
          title: "揽山行-特产",
          id: 4
        },
        component: () => import('@/views/Specialty.vue')
      },
      {
        path: '/search',   // 搜索
        name: 'search',
        meta: {
          title: "揽山行-搜索",
        },
        component: () => import('@/views/Search.vue')
      },
      {
        path: '/person',  // 个人中心
        name: 'person',
        meta: {
          title: "揽山行-个人中心"
        },
        redirect: '/person/article',
        component: () => import('@/views/Person/Person.vue'),
        children: [
          {
            path: '/person/article',  // 个人文章
            name: 'perarticle',
            meta: {
              title: "文章-个人中心",
              num: 1,
              auth: true
            },
            component: () => import('@/views/Person/MyArticle.vue'),
          },
          {
            path: '/person/collection',  // 个人收藏
            name: 'percollection',
            meta: {
              title: "收藏-个人中心",
              num: 2,
              auth: true
            },
            component: () => import('@/views/Person/MyColl.vue'),
          },
          {
            path: '/person/orders',  // 个人订单
            name: 'perorders',
            meta: {
              title: "订单-个人中心",
              num: 3,
              auth: true
            },
            component: () => import('@/views/Person/Orders.vue'),
          },
          {
            path: '/person/settings',  // 个人修改信息
            name: 'persettings',
            meta: {
              title: "修改-个人中心",
              num: 4,
              auth: true
            },
            component: () => import('@/views/Person/Settings.vue'),
          }

        ]
      },
      {
        path: '/recomarticle',  // 推荐文章详情页
        name: 'recomarticle',
        meta: {
          title: "揽山行-推荐文章详情"
        },
        component: () => import('@/views/RecomDetail.vue')
      },
      {
        path: '/commuarticle',  // 社区文章详情页
        name: 'commuarticle',
        meta: {
          title: "揽山行-社区文章详情",
        },
        component: () => import('@/views/CommuDetail.vue')
      }]
  },
  {
    path: '/publish',  // 发布文章
    name: 'publish',
    meta: {
      title: "揽山行-发布文章",
      auth: true
    },
    component: () => import('@/views/Publish.vue')
  },
  {
    path: '/login',  // 登录页面
    name: 'login',
    meta: {
      title: "揽山行-登录"
    },
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',  // 注册页面
    name: 'register',
    meta: {
      title: "揽山行-注册"
    },
    component: () => import('@/views/Register.vue')
  },
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach((to, from, next) => {
  // 跳转到新页面，回到顶部
  document.body.scrollTop = 0
  document.documentElement.scrollTop = 0
  window.scrollTo(0, 0)
  if (to.meta.auth) {
    if (getToken()) {
      next()
    } else {
      $message('warning', '请先登录')
      router.push('/login')
    }
  } else {
    next()
  }
})
export default router
